//
//  BCMineVC.m
//  BangcaiSale
//
//  Created by lsy on 16/7/7.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCMineVC.h"
#import "BCMyCollectionVC.h"
#import "BCMyChargeVC.h"
#import "BCSettingViewController.h"
#import "BCMineHeaderCollectionViewCell.h"
#import "BCMineDataCollectionViewCell.h"
#import "BCMineControlCollectionViewCell.h"
#import "BCMyAppealVC.h"
#import "BCAboutUs.h"
#import "BCInfoVC.h"
#import "BCLogoCollectionViewCell.h"
#import "BCGaryNullCollectionViewCell.h"

@interface BCMineVC () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *mineCollectionView;
//拨号工具web
@property (nonatomic, strong) UIWebView *phoneCallWebView;
@end

@implementation BCMineVC

-(void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_initNavgationBar];
    [self p_initCollectionView];
}

-(void)viewWillAppear:(BOOL)animated {
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = YES;
//    [[BCShopCarManager sharedInstance] hideShopWindow];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSNumber *isFromMianPage_GotoMyCharge = [[NSUserDefaults standardUserDefaults] valueForKey:@"isFromMianPage_GotoMyCharge"];
    if (isFromMianPage_GotoMyCharge.boolValue) {
        BCMyChargeVC *nextViewController = [[BCMyChargeVC alloc] init];
        [self.navigationController pushViewController:nextViewController animated:YES];
    }
}

#pragma mark - View init
- (void)p_initNavgationBar {
    self.title = @"我的";
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mineCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.mineCollectionView.backgroundColor = [UIColor whiteColor];
    self.mineCollectionView.delegate = self;
    self.mineCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mineCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    if (iPhone4_4S) {
        self.mineCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 29, 0);
    }
    self.mineCollectionView.backgroundColor = [UIColor whiteColor];//[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.view addSubview:self.mineCollectionView];
}

#pragma mark - Private Action
-(void)CallPhone{
    
    NSString *phoneNum = @"4000042400";// 电话号码
    
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];
    
    if ( !_phoneCallWebView ) {
        
        _phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];// 这个webView只是一个后台的容易 不需要add到页面上来  效果跟方法二一样 但是这个方法是合法的
        
    }
    
    [_phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else if (section == 1) {
        return 3;
    }
    else if (section == 2) {
        return 1;
    }
    else if(section == 3){
        return 3;
    }
    else {
        return 1;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        static NSString *ID = @"MineTitleCell";
        BCMineHeaderCollectionViewCell *cell = [BCMineHeaderCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        __weak typeof(self) weakSelf = self;
        cell.PushToSetting = ^() {
            BCSettingViewController *nextViewController = [[BCSettingViewController alloc] init];
            [weakSelf.navigationController pushViewController:nextViewController animated:YES];
        };
        return cell;
    }
    else if (indexPath.section == 1) {
        
        static NSString *ID = @"MineDataCell";
        BCMineDataCollectionViewCell *cell = [BCMineDataCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            cell.titleLable.text = @"本周收入";
            cell.valueLable.text = @"￥100";
            cell.valueLable.textColor = [UIColor colorWithRed:238.f/255.f green:81.f/255.f blue:58.f/255.f alpha:1];
            cell.rightLineImageView.hidden = NO;
        }
        else if (indexPath.row == 1) {
            cell.titleLable.text = @"我的积分";
            cell.valueLable.text = @"9000";
            cell.valueLable.textColor = [UIColor colorWithRed:100.f/255.f green:190.f/255.f blue:125.f/255.f alpha:1];
            cell.rightLineImageView.hidden = NO;
        }
        else {
            cell.titleLable.text = @"收到申诉";
            cell.valueLable.text = @"3";
            cell.valueLable.textColor = [UIColor colorWithRed:238.f/255.f green:81.f/255.f blue:58.f/255.f alpha:1];
            cell.rightLineImageView.hidden = YES;
        }
        
        return cell;
    }
    else if (indexPath.section == 2) {
        static NSString *ID = @"MineGaryNullCellIdentifer";
        BCGaryNullCollectionViewCell *cell = [BCGaryNullCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
        
    }
    else if(indexPath.section == 3){
        
        static NSString *ID = @"MineControlCell";
        BCMineControlCollectionViewCell *cell = [BCMineControlCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            cell.titleLable.text = @"我的收藏";
            cell.iconImageView.image = [UIImage imageNamed:@"心"];
            cell.nextImageView.hidden = NO;
            cell.telLable.hidden = YES;
        }
        else if (indexPath.row == 1) {
            cell.titleLable.text = @"客服电话";
            cell.iconImageView.image = [UIImage imageNamed:@"电话"];
            cell.nextImageView.hidden = YES;
            cell.telLable.hidden = NO;
        }
        else {
            cell.titleLable.text = @"关于我们";
            cell.iconImageView.image = [UIImage imageNamed:@"关于"];
            cell.nextImageView.hidden = NO;
            cell.telLable.hidden = YES;
        }
        
        return cell;
    }
    else {
        static NSString *ID = @"MineLogoCell";
        BCLogoCollectionViewCell *cell = [BCLogoCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (SCREEN_WIDTH<=320.f) {
            cell.logoImageView.hidden = YES;
        }
        else {
            cell.logoImageView.hidden = NO;
        }
        return cell;
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    if (section == 0) {
        UIEdgeInsets top = {0,0,0,0};
        return top;
    }
    else if(section == 1) {
        UIEdgeInsets top = {0,0,0,0};
        return top;
    }
    else if(section == 2) {
        UIEdgeInsets top = {0,0,0,0};
        return top;
    }
    else if(section == 3){
        UIEdgeInsets top = {0,1,0,1};
        return top;
    }
    else {
        UIEdgeInsets top = {0,0,0,0};
        return top;
    }
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(self.view.frame.size.width, 198.f);
    }
    else if (indexPath.section == 1) {
        return CGSizeMake(self.view.frame.size.width/3, 71.f);
    }
    else if (indexPath.section == 2) {
        return CGSizeMake(self.view.frame.size.width, 5.f);
    }
    else if(indexPath.section == 3){
        return CGSizeMake(self.view.frame.size.width-2.f, 50.f);
    }
    else {
        CGFloat height = self.view.frame.size.height-198.f-71.f-10.f-(50.f*4);
        if (height<0) {
            height = 0;
        }
        return CGSizeMake(self.view.frame.size.width, height);
    }
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        BCInfoVC *userInfo = [[BCInfoVC alloc] init];
        [self.navigationController pushViewController:userInfo animated:YES];
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            BCMyChargeVC *nextViewController = [[BCMyChargeVC alloc] init];
            [self.navigationController pushViewController:nextViewController animated:YES];
        }
        else if (indexPath.row == 1) {
        }
        else {
            BCMyAppealVC *nextViewController = [[BCMyAppealVC alloc] init];
            [self.navigationController pushViewController:nextViewController animated:YES];
        }
    }
    else if (indexPath.section == 2) {
    }
    else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            BCMyCollectionVC *nextViewController = [[BCMyCollectionVC alloc] init];
            [self.navigationController pushViewController:nextViewController animated:YES];
        }
        else if (indexPath.row == 1) {
            [self CallPhone];
        }
        else if(indexPath.row == 2){
            BCAboutUs *nextViewController = [[BCAboutUs alloc] init];
            [self.navigationController pushViewController:nextViewController animated:YES];
        }
    }
    else {
    }
}

@end